home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Plus 2000 #1
/
Amiga Plus CD - 2000 - No. 1.iso
/
Tools
/
Misc
/
InstallerNG
/
examples
/
math.installer
next >
Wrap
Text File
|
1999-12-03
|
4KB
|
151 lines
(user expert)
(set @proceed-button "Interesting... go on!"
@abort-button "Urgs"
)
(message "\n\n\nWelcome to some recursive math...\n\n\n"
"Please have a look at the source to see how to\n"
"program recursive functions and how to use\n"
"local environments of procedures!"
"\n\nNote: send CTRL-F to the installer process,\n"
"if you want to stop interpretation"
)
; --------------------------------------------------------------------------------
; define a recursive procedure which calculates the faculty of
; a given argument. use the LET function to create a local environment
; note: for arguments greater than 12 there will be an overflow,
; but this does not result in any runtime error
(procedure faculty a
(set fac_number (+ 1 fac_number))
(let (set f a)
(if (= f 1)
1
(* f (faculty (- f 1)))
)
)
)
; --------------------------------------------------------------------------------
; the function "Ackerman" is a extremly recursive function, which only
; runs with local variables. thus here you must use the LET function
;
; the definition of the ackerman function is:
; a(0,y) = y+1
; a(x,0) = a(x-1,1)
; a(x,y) = a(x-1,a(x,y-1))
(procedure ackerman a b
(set ack_number (+ 1 ack_number))
(let (set x a y b)
(if (= x 0)
(+ y 1)
(if (= y 0)
(ackerman (- x 1) 1)
(ackerman (- x 1) (ackerman x (- y 1)))
)
)
)
)
; --------------------------------------------------------------------------------
; this is the well known function "fibonacci"; it is formally defined
; as follows:
;
; fib(0) = 1
; fib(1) = 1
; fib(n) = fib(n-1) + fib(n-2)
(procedure fibonacci f
(set fib_number (+ 1 fib_number))
(let (set n f)
(if (= n 0) 1
(if (= n 1) 1
(+ (fibonacci (- n 1)) (fibonacci (- n 2)))
)
)
)
)
; --------------------------------------------------------------------------------
; ask for the values
(set fac 3
fib 5
)
(swing
(set fac (asknumber (prompt "Faculty of what ?")
(help "enter the number you want to know the faculty of")
(default fac)
(range 1 12)
)
)
(set fib (asknumber (prompt "Fibonacci of what ?")
(help "enter the number you want to know the fibonacci number of")
(default fib)
(range 1 25)
)
)
(message "Done? So lets start the evaluation...")
)
; --------------------------------------------------------------------------------
; calculate and print the faculty
(working (cat "Please be patient... this really could take a while\n"
"(depends on the given values....)\n\n"
"Send CTRL-F to break"
)
)
(set start_time (database "time"))
(set fac_number 0
fib_number 0
ack_number 0
)
(complete 0)
(set result_fac (faculty fac))
(complete 33)
(set result_fib (fibonacci fib))
(complete 66)
(set result_ack (ackerman 1 2))
(set end_time (database "time"))
(complete 100)
(message "Faculty of " fac " = " result_fac "\n"
"Fibonacci of " fib " = " result_fib "\n"
"Ackerman(1,2) = " result_ack "\n\n"
"Took from " start_time " until " end_time "\n\n"
"\n----------\n"
"Function call statistics: \n"
"Faculty " fac_number " times\n"
"Fibonacci " fib_number " times\n"
"Ackerman " ack_number " times"
)
; --------------------------------------------------------------------------------
; avoid stupid welcome... :)
(exit (quiet))
(welcome)